// pages/list/list.js
const API = require('../../utils/api.js');
const TOTAS = require('../../utils/toast.js');
const FormatUtil = require('../../utils/util.js')
const AJAX = require('../../utils/ajax.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNumber: 0,
    pageSize: 10,
    imageHost: API.api.imageHost, // 图片服务器域名
    bookkeepingList: [],
    assetsDetail: {},
    totalElements: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() { 

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 初始化数据信息
    this.setData({
      bookkeepingList: [],
      pageNumber: 0,
      totalElements: 0
    })
    // 获取用户记账信息
    this.getBookkeeping();
    // 获取用户本月消费情况
    this.getAssetsDetail();
  },
  /**
   * 跳转到记账详情页
   */
  gotoDetail: function(e) {
    let bookkeepingId = e.currentTarget.dataset.id
    console.log(`当前获取到的id是：` + bookkeepingId)
    wx.navigateTo({
      url: '/pages/index/bookkeepingdetail/bookkeepingdetail?form=index&bookkeepingid=' + bookkeepingId
    })

  },
  getAssetsDetail: function() {
    let that = this
    let userId = wx.getStorageSync('userId');
    let requestUrl = API.api.assetsDetails + '/' + userId
    AJAX.getRequest(requestUrl).then(function(response) {
      let resData = response.data;
      // 金额转换
      resData.monthTotalExpenditure = FormatUtil.formatMoney(resData.monthTotalExpenditure / 100);
      resData.monthTotalIncome = FormatUtil.formatMoney(resData.monthTotalIncome / 100);
      resData.monthTotalMoney = FormatUtil.formatMoney(resData.monthTotalMoney / 100);
      that.setData({
        assetsDetail: resData
      })
    })
  },
  getBookkeeping: function() {
    console.log(`pageNumber:` + this.data.pageNumber)
    if ((this.data.pageNumber * this.data.pageSize) > this.data.totalElements && this.data.pageNumber != 0) {
      TOTAS.showToastSuccess("已经加载到底了....")
      return
    }
    let that = this;
    let userId = wx.getStorageSync('userId');
    let pageNumber = this.data.pageNumber;
    let pageSize = this.data.pageSize;
    let requestUrl = API.api.bookkeepings + '?userId=' + userId + '&pageNumber=' + pageNumber + '&pageSize=' + pageSize;
    AJAX.getRequest(requestUrl).then(response => {
      if (response.data.content.length == 0 && this.data.pageNumber != 0) {
        TOTAS.showToastSuccess("已经加载到底了....")
        return
      }
      // 格式化数据
      response.data.content.forEach(item => {
        item.money = FormatUtil.formatMoney(item.money / 100);
        item.totalMoney = FormatUtil.formatMoney(item.totalMoney / 100);
      })
      let bookkeepingList = [];
      if (that.data.bookkeepingList.length == 0) {
        // 直接赋值
        bookkeepingList = response.data.content;
      } else {
        bookkeepingList = that.data.bookkeepingList;
        response.data.content.forEach(item => {
          bookkeepingList.push(item)
        })
      }
      that.setData({
        bookkeepingList: bookkeepingList,
        pageNumber: pageNumber + 1,
        totalElements: response.data.totalElements
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // 触发到底部。进行加载数据信息
    this.getBookkeeping();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})